package com.training;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;

import com.mysql.jdbc.Driver;
import com.training.dao.EmployeeDAO;
import com.training.dto.Employee;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	ServletConfig servletConfig = null;

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		
		//from Vasu
		
		return this.servletConfig;


	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {

		servletConfig = config;

	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#getServletInfo()
	 */
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

 		
		String nameInput = request.getParameter("name").trim();
		String passwordInput = request.getParameter("password").trim();

		System.out.println("Name is -- " + nameInput);
		System.out.println("Password is --- " + passwordInput);

		if (nameInput != null && passwordInput != null) {

			EmployeeDAO loginDAO = new EmployeeDAO();
			Employee employee = loginDAO.validateEmployeeLogin(nameInput,
					passwordInput);

			if (employee == null) {
				
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("login.jsp");
				request.setAttribute("errorMessage", "Please enter Valid User Name/Pasword");

				dispatcher.forward(request, response);
				
			} else {

				HttpSession session = request.getSession();
				session.setAttribute("employee", employee);
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("welcomePage.jsp");

				dispatcher.forward(request, response);

			}
		} else {
			response.sendRedirect("registration.html");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
